[id].tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import { Image } from 'expo-image';
  2. import { useLocalSearchParams, useRouter } from 'expo-router';
  3. import React, { useCallback, useEffect, useRef, useState } from 'react';
  4. import {
  5. ActivityIndicator,
  6. Dimensions,
  7. ImageBackground,
  8. ScrollView,
  9. StatusBar,
  10. StyleSheet,
  11. Text,
  12. TouchableOpacity,
  13. View
  14. } from 'react-native';
  15. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  16. import { CheckoutModal } from '@/components/product/CheckoutModal';
  17. import { Images } from '@/constants/images';
  18. import { getGoodsDetail, GoodsDetail, previewSubmit } from '@/services/mall';
  19. const { width: SCREEN_WIDTH } = Dimensions.get('window');
  20. export default function ProductDetailScreen() {
  21. const { id, subjectId } = useLocalSearchParams<{ id: string; subjectId?: string }>();
  22. const router = useRouter();
  23. const insets = useSafeAreaInsets();
  24. const checkoutRef = useRef<any>(null);
  25. const [loading, setLoading] = useState(true);
  26. const [data, setData] = useState<GoodsDetail | null>(null);
  27. // 加载商品详情
  28. const loadData = useCallback(async () => {
  29. if (!id) return;
  30. setLoading(true);
  31. try {
  32. const detail = await getGoodsDetail(id, subjectId);
  33. setData(detail);
  34. } catch (error) {
  35. console.error('加载商品详情失败:', error);
  36. }
  37. setLoading(false);
  38. }, [id, subjectId]);
  39. useEffect(() => {
  40. loadData();
  41. }, [loadData]);
  42. // 显示结算弹窗
  43. const showCheckout = async () => {
  44. if (!data) return;
  45. try {
  46. const preview = await previewSubmit({
  47. goodsId: id!,
  48. quantity: 1,
  49. subjectId,
  50. });
  51. if (preview) {
  52. checkoutRef.current?.show(preview);
  53. }
  54. } catch (error) {
  55. console.error('预提交失败:', error);
  56. }
  57. };
  58. // 返回
  59. const goBack = () => {
  60. router.back();
  61. };
  62. if (loading) {
  63. return (
  64. <View style={styles.loadingContainer}>
  65. <ActivityIndicator size="large" color="#fff" />
  66. </View>
  67. );
  68. }
  69. if (!data) {
  70. return (
  71. <View style={styles.loadingContainer}>
  72. <Text style={styles.errorText}>商品不存在</Text>
  73. </View>
  74. );
  75. }
  76. return (
  77. <View style={styles.container}>
  78. <StatusBar barStyle="light-content" />
  79. <ImageBackground
  80. source={{ uri: Images.mine.kaixinMineBg }}
  81. style={styles.background}
  82. resizeMode="cover"
  83. >
  84. {/* 顶部导航 */}
  85. <View style={[styles.header, { paddingTop: insets.top }]}>
  86. <TouchableOpacity style={styles.backBtn} onPress={goBack}>
  87. <Text style={styles.backText}>←</Text>
  88. </TouchableOpacity>
  89. <Text style={styles.headerTitle}>商品详情</Text>
  90. <View style={styles.placeholder} />
  91. </View>
  92. <ScrollView style={styles.scrollView} showsVerticalScrollIndicator={false}>
  93. {/* 商品图片 */}
  94. <View style={styles.imageWrapper}>
  95. <Image source={data.spu.cover} style={styles.coverImage} contentFit="cover" />
  96. </View>
  97. {/* 商品信息 */}
  98. <ImageBackground
  99. source={{ uri: Images.common.itemBg }}
  100. style={styles.infoSection}
  101. resizeMode="stretch"
  102. >
  103. <View style={styles.priceRow}>
  104. <Text style={styles.currency}>¥</Text>
  105. <Text style={styles.price}>{data.subjectPrice || data.price}</Text>
  106. {data.sellType === 2 && (
  107. <View style={styles.presellTag}>
  108. <Text style={styles.presellText}>预售</Text>
  109. </View>
  110. )}
  111. </View>
  112. <Text style={styles.name}>{data.spu.name}</Text>
  113. </ImageBackground>
  114. {/* 商品详情图片 */}
  115. {data.spu.images && data.spu.images.length > 0 && (
  116. <ImageBackground
  117. source={{ uri: Images.common.itemBg }}
  118. style={styles.detailSection}
  119. resizeMode="stretch"
  120. >
  121. <Text style={styles.sectionTitle}>商品详情</Text>
  122. {data.spu.images.map((img, index) => (
  123. <Image key={index} source={img} style={styles.detailImage} contentFit="contain" />
  124. ))}
  125. </ImageBackground>
  126. )}
  127. {/* 推荐商品 */}
  128. {data.recommendedMallGoods && data.recommendedMallGoods.length > 0 && (
  129. <ImageBackground
  130. source={{ uri: Images.common.itemBg }}
  131. style={styles.recommendSection}
  132. resizeMode="stretch"
  133. >
  134. <Text style={styles.sectionTitle}>推荐商品</Text>
  135. <ScrollView horizontal showsHorizontalScrollIndicator={false}>
  136. {data.recommendedMallGoods.map((item) => (
  137. <TouchableOpacity
  138. key={item.id}
  139. style={styles.recommendItem}
  140. onPress={() => router.push(`/product/${item.id}` as any)}
  141. >
  142. <Image source={item.cover} style={styles.recommendImage} contentFit="cover" />
  143. <Text style={styles.recommendName} numberOfLines={1}>{item.name}</Text>
  144. <Text style={styles.recommendPrice}>¥{item.price}</Text>
  145. </TouchableOpacity>
  146. ))}
  147. </ScrollView>
  148. </ImageBackground>
  149. )}
  150. <View style={styles.bottomSpace} />
  151. </ScrollView>
  152. {/* 底部购买栏 */}
  153. <View style={[styles.bottomBar, { paddingBottom: insets.bottom + 10 }]}>
  154. <TouchableOpacity style={styles.serviceBtn}>
  155. <Text style={styles.serviceBtnText}>客服</Text>
  156. </TouchableOpacity>
  157. <TouchableOpacity style={styles.buyBtn} onPress={showCheckout} activeOpacity={0.8}>
  158. <ImageBackground
  159. source={{ uri: Images.common.loginBtn }}
  160. style={styles.buyBtnBg}
  161. resizeMode="stretch"
  162. >
  163. <Text style={styles.buyBtnText}>
  164. {data.sellType === 2 ? '支付定金' : '立即购买'}
  165. </Text>
  166. </ImageBackground>
  167. </TouchableOpacity>
  168. </View>
  169. </ImageBackground>
  170. {/* 结算弹窗 */}
  171. <CheckoutModal
  172. ref={checkoutRef}
  173. data={data}
  174. goodsId={id!}
  175. subjectId={subjectId}
  176. />
  177. </View>
  178. );
  179. }
  180. const styles = StyleSheet.create({
  181. container: {
  182. flex: 1,
  183. backgroundColor: '#1a1a2e',
  184. },
  185. background: {
  186. flex: 1,
  187. },
  188. loadingContainer: {
  189. flex: 1,
  190. backgroundColor: '#1a1a2e',
  191. justifyContent: 'center',
  192. alignItems: 'center',
  193. },
  194. errorText: {
  195. color: '#999',
  196. fontSize: 16,
  197. },
  198. header: {
  199. flexDirection: 'row',
  200. alignItems: 'center',
  201. justifyContent: 'space-between',
  202. paddingHorizontal: 15,
  203. paddingBottom: 10,
  204. },
  205. backBtn: {
  206. width: 40,
  207. height: 40,
  208. justifyContent: 'center',
  209. alignItems: 'center',
  210. },
  211. backText: {
  212. color: '#fff',
  213. fontSize: 24,
  214. },
  215. headerTitle: {
  216. color: '#fff',
  217. fontSize: 18,
  218. fontWeight: '600',
  219. },
  220. placeholder: {
  221. width: 40,
  222. },
  223. scrollView: {
  224. flex: 1,
  225. },
  226. imageWrapper: {
  227. width: SCREEN_WIDTH,
  228. height: SCREEN_WIDTH,
  229. backgroundColor: '#fff',
  230. },
  231. coverImage: {
  232. width: '100%',
  233. height: '100%',
  234. },
  235. infoSection: {
  236. padding: 15,
  237. marginHorizontal: 10,
  238. marginTop: 10,
  239. borderRadius: 12,
  240. overflow: 'hidden',
  241. },
  242. priceRow: {
  243. flexDirection: 'row',
  244. alignItems: 'baseline',
  245. },
  246. currency: {
  247. color: '#ff6b00',
  248. fontSize: 14,
  249. },
  250. price: {
  251. color: '#ff6b00',
  252. fontSize: 28,
  253. fontWeight: 'bold',
  254. },
  255. presellTag: {
  256. backgroundColor: '#8b3dff',
  257. borderRadius: 12,
  258. paddingHorizontal: 10,
  259. paddingVertical: 3,
  260. marginLeft: 10,
  261. },
  262. presellText: {
  263. color: '#fff',
  264. fontSize: 12,
  265. },
  266. name: {
  267. color: '#333',
  268. fontSize: 16,
  269. marginTop: 10,
  270. lineHeight: 22,
  271. },
  272. detailSection: {
  273. marginTop: 10,
  274. marginHorizontal: 10,
  275. padding: 15,
  276. borderRadius: 12,
  277. overflow: 'hidden',
  278. },
  279. sectionTitle: {
  280. color: '#333',
  281. fontSize: 16,
  282. fontWeight: '600',
  283. marginBottom: 15,
  284. },
  285. detailImage: {
  286. width: SCREEN_WIDTH - 50,
  287. height: 300,
  288. marginBottom: 10,
  289. },
  290. recommendSection: {
  291. marginTop: 10,
  292. marginHorizontal: 10,
  293. padding: 15,
  294. borderRadius: 12,
  295. overflow: 'hidden',
  296. },
  297. recommendItem: {
  298. width: 120,
  299. marginRight: 10,
  300. },
  301. recommendImage: {
  302. width: 120,
  303. height: 120,
  304. borderRadius: 8,
  305. backgroundColor: '#fff',
  306. },
  307. recommendName: {
  308. color: '#333',
  309. fontSize: 12,
  310. marginTop: 8,
  311. },
  312. recommendPrice: {
  313. color: '#ff6b00',
  314. fontSize: 14,
  315. fontWeight: '600',
  316. marginTop: 4,
  317. },
  318. bottomSpace: {
  319. height: 100,
  320. },
  321. bottomBar: {
  322. position: 'absolute',
  323. bottom: 0,
  324. left: 0,
  325. right: 0,
  326. flexDirection: 'row',
  327. alignItems: 'center',
  328. paddingHorizontal: 15,
  329. paddingTop: 10,
  330. backgroundColor: 'rgba(0,0,0,0.3)',
  331. },
  332. serviceBtn: {
  333. paddingHorizontal: 25,
  334. paddingVertical: 12,
  335. backgroundColor: 'rgba(255,255,255,0.9)',
  336. borderRadius: 25,
  337. },
  338. serviceBtnText: {
  339. color: '#333',
  340. fontSize: 14,
  341. },
  342. buyBtn: {
  343. flex: 1,
  344. marginLeft: 15,
  345. height: 45,
  346. overflow: 'hidden',
  347. },
  348. buyBtnBg: {
  349. width: '100%',
  350. height: '100%',
  351. justifyContent: 'center',
  352. alignItems: 'center',
  353. },
  354. buyBtnText: {
  355. color: '#fff',
  356. fontSize: 16,
  357. fontWeight: '600',
  358. },
  359. });